﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using ByteSite.Common;
using System.Data;
using System.Linq.Expressions;

namespace ByteSite.Web.UI.Page
{
    public partial class article_show : Web.UI.BasePage
    {
        protected static string channel = string.Empty; //频道名称
        protected static int id;
        protected static string page = string.Empty;
        protected static Entitys.article model = new Entitys.article();
        
        /// <summary>
        /// 重写虚方法,此方法将在Init事件前执行
        /// </summary>
        protected override void ShowPage()
        {
            this.Init += new EventHandler(article_show_Init);
        }

        /// <summary>
        /// OnInit事件,让频道名称变量先赋值
        /// </summary>
        void article_show_Init(object sender, EventArgs e)
        {
           
        }

        /// <summary>
        /// 设置模板值
        /// </summary>
        /// <param name="template"></param>
        /// <returns></returns>
        protected static JinianNet.JNTemplate.ITemplate SetData(JinianNet.JNTemplate.ITemplate template)
        {
            id = DTRequest.GetQueryInt("id");
            page = DTRequest.GetQueryString("page");
            BLL.article bll = new BLL.article();

            if (id > 0) //如果ID获取到，将使用ID
            {
                if (!bll.ArticleExists(channel, id))
                {
                    HttpContext.Current.Response.Redirect(LinkUrlS("error", "?msg=" + Utils.UrlEncode("出错啦，您要浏览的页面不存在或已删除！")));
                }
                model = bll.ArticleModel(channel, id);
            }
            else if (!string.IsNullOrEmpty(page)) //否则检查设置的别名
            {
                if (!bll.ArticleExists(channel, page))
                {
                    HttpContext.Current.Response.Redirect(LinkUrlS("error", "?msg=" + Utils.UrlEncode("出错啦，您要浏览的页面不存在或已删除！")));
                }
                model = bll.ArticleModel(channel, page);
            }
            //跳转URL
            if (model.link_url != null)
            {
                model.link_url = model.link_url.Trim();
            }
            if (!string.IsNullOrEmpty(model.link_url))
            {
                HttpContext.Current.Response.Redirect(model.link_url);
            }

            template.Set("page", page);
            template.Set("id", id);
            template.Set("model", model);

            return template;
        }

        /// <summary>
        /// 获取上一条下一条的链接
        /// </summary>
        /// <param name="urlkey">urlkey</param>
        /// <param name="type">-1代表上一条，1代表下一条</param>
        /// <param name="defaultvalue">默认文本</param>
        /// <param name="callIndex">是否使用别名，0使用ID，1使用别名</param>
        /// <returns>A链接</returns>
        protected string get_prevandnext_article(string urlkey, int type, string defaultvalue, int callIndex)
        {
            string symbol = (type == -1 ? "<" : ">");
            Expression<Func<Entitys.article, bool>> expression = t => true;
            if (type == -1)
            {
                expression = expression.And(x => x.id < id);
            } else
            {
                expression = expression.And(x => x.id > id);
            }
            expression = expression.And(x => x.channel_id == model.channel_id && x.status == 0 && x.category_id == model.category_id);

            BLL.article bll = new BLL.article();

            string orderby = type == -1 ? "id desc" : "id asc";
            List<Entitys.article> list = bll.ArticleList(channel, 1, expression, orderby);
            if (list == null || list.Count <= 0)
            {
                return defaultvalue;
            }
            if (callIndex == 1 && !string.IsNullOrEmpty(list[0].call_index))
            {
                return "<a href=\"" + LinkUrl(urlkey, list[0].call_index) + "\">" + list[0].title + "</a>";
            }
            return "<a href=\"" + LinkUrl(urlkey, list[0].id.ToString()) + "\">" + list[0].title + "</a>";
        }
    }
}
